/*全局配置axios*/
//导入axios
import axios from 'axios'
import router from "../router";
import ElementUI from "element-ui";

//设置访问根路径
axios.defaults.baseURL='/api/'

axios.interceptors.request.use((config) => {
    let token = sessionStorage.getItem('token')
    token && (config.headers.token  = token);//请求携带token
    return config
})


let flag = true //节流
axios.interceptors.response.use(res => {
    if (flag && res.data.code === "-1") {
        ElementUI.Message.warning("登录过期，请重新登录")
        router.push("/login")
        flag = false
        setTimeout(() => {flag = true}, 1000)
        return res.data
    }
    return res.data
}, error => {
    console.log(error)
})

export default axios